//
//  MLAShopOrderVC.m
//  MagicLampApp
//
//  Created by Zheng on 2019/4/12.
//  Copyright © 2019 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAShopOrderVC.h"
#import "MLAEateryOrderCell.h"
#import "MLAEateryOrderModel.h"

@interface MLAShopOrderVC ()

@end

@implementation MLAShopOrderVC
{
    NSMutableArray<MLAEateryOrderModel *> *modelsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.rowHeight = 175;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"MLAEateryOrderCell" bundle:nil] forCellReuseIdentifier:@"kCellID"];
    self.haveRefreshHeader = YES;
    [self loadData];
}

#pragma mark - LoadData
- (void)loadData {
    [super loadData];
    
    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:kShopOrderList params:@{@"state":_type} success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self->modelsArr = [NSMutableArray array];
        self.page++;
        self.haveRefreshFooter = YES;
        self->modelsArr = [NSMutableArray array];
        self->modelsArr = [MLAEateryOrderModel mj_objectArrayWithKeyValuesArray:response[@"lists"]];
        if (self->modelsArr.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
        [self.tableView reloadData];
    } failure:^{
        
    }];
    
}

- (void)loadMoreData {
    NSDictionary *params = @{@"page":@(self.page),
                             @"state":_type};
    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:kShopOrderList params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self.page++;
        if ([response[@"lists"] count] < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        [self->modelsArr addObjectsFromArray:[MLAEateryOrderModel mj_objectArrayWithKeyValuesArray:response[@"lists"]]];
        [self.tableView reloadData];
    } failure:^{
        
    }];
}


#pragma mark - tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return modelsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MLAEateryOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kCellID" forIndexPath:indexPath];
    MLAEateryOrderModel *model = modelsArr[indexPath.row];
    cell.model = model;
    @weakify(self);
    cell.finishAction = ^(MLAEateryOrderModel * _Nonnull model) {
        @strongify(self);
        [self finishAction:model];
    };
    return cell;
}

#pragma mark - Actions
- (void)finishAction:(MLAEateryOrderModel *)model {
    
    NSDictionary *param = @{@"id":model.modelId,
                            @"state":@"4"};
    
    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:kShopOrderSave params:param success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        
        model.state = @"4";
        
        [self.tableView reloadData];
    } failure:^{
        
    }];
}

@end
